package activity.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConn2 {
    public Connection getConn(){
        Connection conn=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException exception) {
            exception.printStackTrace();
            System.out.println("加载数据库驱动异常");
            return null;
        }
        String url="jdbc:mysql://localhost:3306/actKu";
        String user="root";
        String password="123456";
        try {
            conn= DriverManager.getConnection(url,user,password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            System.out.println("连接异常");
            return null;
        }
        System.out.println("连接成功");
        return conn;
    }
    public void closeConn(Connection conn){
        if(conn!=null){
            try {
                conn.close();
                System.out.println("已关闭数据库");
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        admin.utils.DBConn obj=new admin.utils.DBConn();
        Connection conn= obj.getConn();
        obj.closeConn(conn);
    }
}